﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace ArchSoft
{
    public partial class ProjectInfFrm : Form
    {
        public ProjectInfFrm()
        {
            InitializeComponent();
        }

        #region " Propertiese "

        string ProjectPath_ = "";
        public string projectPath
        {
            set { this.ProjectPath_ = value; }
        }

        #endregion       

        void LoadImage(string ImgPath)
        {
            this.PsPic.Image = null;
            if (this.PsPic.Left != 316 || this.PsPic.Top != 179)
            {
                this.PsPic.Location = new Point(316, 179);
                this.PsPic.Size = new Size(100, 100);
            }


            Image img = Image.FromFile(ImgPath);          //Location: 316, 179!
            int ix = img.Width;
            int iy = img.Height;
            if (ix > iy)
            {
                ix = 100;
                iy = 100 * iy / img.Width;
                PsPic.Height = iy;
                PsPic.Top += (100 - iy) / 2;
            }
            else
            {
                iy = 100;
                ix = 100 * ix / img.Height;
                PsPic.Width = ix;
                PsPic.Left += (100 - ix) / 2;
            }
            this.PsPic.Image = img;
        }

        private void ProjectInfFrm_Load(object sender, EventArgs e)
        {
            //Load General!!
            try
            {
                this.Comb_Size.SelectedIndex = 0;
                if (File.Exists(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE))
                    LoadImage(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE);
                //Genn!
                int nof = 0;

                foreach (string Fol in Directory.GetDirectories(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_OUTPUT_FOLDER))
                {
                    if (!Fol.EndsWith("Xref"))
                    {
                        foreach (string Fl in Directory.GetFiles(Fol))
                        {
                            if (Fl.EndsWith(".dwg"))
                            {
                                nof += 1;
                            }
                        }
                    }
                }

                this.Tbx_Location.Text = ProjectPath_;
                this.La_ProName.Text = Path.GetFileNameWithoutExtension(ProjectPath_);
                this.La_NoDs.Text = nof + " Drawing(s)";
                Date_Start.Value = DateTime.Today;
                Date_End.Value = DateTime.Today;


                //Load Noi dung File Dinh nghia(neu ton tai)!!!
                string cpath = this.ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
                DataTable tbConsl = null;
                if (File.Exists(cpath))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(cpath);
                    tbConsl = ds.Tables[0];
                    //Load Consultant!
                    if (tbConsl != null)
                    {
                        //string a = EncodeDecode.GetDecoded(tbConsl.Rows[0][1].ToString()).TrimEnd(new char[] { '\0' });
                        //Gene!!                        
                        Date_Start.Value = DateTime.ParseExact(EncodeDecode.GetDecoded(tbConsl.Rows[0][1].ToString()).TrimEnd(new char[] { '\0' }), "MM/dd/yyyy", null);
                        Date_End.Value = DateTime.ParseExact(EncodeDecode.GetDecoded(tbConsl.Rows[1][1].ToString()).TrimEnd(new char[] { '\0' }), "MM/dd/yyyy", null);
                        Comb_Type.Text = EncodeDecode.GetDecoded(tbConsl.Rows[2][1].ToString());
                        Comb_Status.Text = EncodeDecode.GetDecoded(tbConsl.Rows[3][1].ToString());
                        La_LstSave.Text = EncodeDecode.GetDecoded(tbConsl.Rows[4][1].ToString());
                        Tbx_Desc.Text = EncodeDecode.GetDecoded(tbConsl.Rows[5][1].ToString());
                        Comb_Size.Text = EncodeDecode.GetDecoded(tbConsl.Rows[20][1].ToString());

                        //Con!!
                        Tbx_Con_Cli.Text = EncodeDecode.GetDecoded(tbConsl.Rows[6][1].ToString());
                        Tbx_Con_Mana.Text = EncodeDecode.GetDecoded(tbConsl.Rows[7][1].ToString());
                        Tbx_Con_Arc.Text = EncodeDecode.GetDecoded(tbConsl.Rows[8][1].ToString());
                        Tbx_Con_Int.Text = EncodeDecode.GetDecoded(tbConsl.Rows[9][1].ToString());
                        Tbx_con_Land.Text = EncodeDecode.GetDecoded(tbConsl.Rows[10][1].ToString());
                        Tbx_Con_Struc.Text = EncodeDecode.GetDecoded(tbConsl.Rows[11][1].ToString());
                        Tbx_Con_ME.Text = EncodeDecode.GetDecoded(tbConsl.Rows[12][1].ToString());
                        Tbx_Con_QS.Text = EncodeDecode.GetDecoded(tbConsl.Rows[13][1].ToString());
                        Tbx_Con_Comm.Text = EncodeDecode.GetDecoded(tbConsl.Rows[14][1].ToString());

                        //Dev!!
                        Tbx_Dev_Appr.Text = EncodeDecode.GetDecoded(tbConsl.Rows[15][1].ToString());
                        Tbx_Dev_chck.Text = EncodeDecode.GetDecoded(tbConsl.Rows[16][1].ToString());
                        Tbx_Dev_Des.Text = EncodeDecode.GetDecoded(tbConsl.Rows[17][1].ToString());
                        Tbx_Dev_Drw.Text = EncodeDecode.GetDecoded(tbConsl.Rows[18][1].ToString());
                        Tbx_Dev_Comm.Text = EncodeDecode.GetDecoded(tbConsl.Rows[19][1].ToString());
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        //Saving!
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Date_End.Value < this.Date_Start.Value)
                {
                    MessageBox.Show("Ngày kết thúc phải sau hơn ngày bắt đầu ", "Ghi chú", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Date_End.Focus();
                    Date_End.Select();
                    return;
                }
                //Tao Dong Du lieu Moi Cho SumTbl va Save!
                DataTable SumTbl = new DataTable("ProjectInf.INF");
                SumTbl.Columns.Add("KeyInput");
                SumTbl.Columns.Add("KeyValue");
                SumTbl.Constraints.Add("Key", SumTbl.Columns[0], true); //*_Kh*_Wh*V/iA/ûû
                

                //Gene!!               
                SumTbl.Rows.Add(new string[] { Date_Start.Tag.ToString(), EncodeDecode.GetEncoded(Date_Start.Value.ToString("MM/dd/yyyy")) });
                SumTbl.Rows.Add(new string[] { Date_End.Tag.ToString(), EncodeDecode.GetEncoded(Date_End.Value.ToString("MM/dd/yyyy")) });
                SumTbl.Rows.Add(new string[] { Comb_Type.Tag.ToString(), EncodeDecode.GetEncoded(Comb_Type.Text) });
                SumTbl.Rows.Add(new string[] { Comb_Status.Tag.ToString(), EncodeDecode.GetEncoded(Comb_Status.Text) });
                SumTbl.Rows.Add(new string[] { La_LstSave.Tag.ToString(), EncodeDecode.GetEncoded(DateTime.Now.ToString()) });
                SumTbl.Rows.Add(new string[] { Tbx_Desc.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Desc.Text) });

                //Con!!
                SumTbl.Rows.Add(new string[] { Tbx_Con_Cli.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_Cli.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_Mana.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_Mana.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_Arc.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_Arc.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_Int.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_Int.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_con_Land.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_con_Land.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_Struc.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_Struc.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_ME.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_ME.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_QS.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_QS.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Con_Comm.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Con_Comm.Text) });

                //Dev!!
                SumTbl.Rows.Add(new string[] { Tbx_Dev_Appr.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Dev_Appr.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Dev_chck.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Dev_chck.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Dev_Des.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Dev_Des.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Dev_Drw.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Dev_Drw.Text) });
                SumTbl.Rows.Add(new string[] { Tbx_Dev_Comm.Tag.ToString(), EncodeDecode.GetEncoded(Tbx_Dev_Comm.Text) });
                SumTbl.Rows.Add(new string[] { Comb_Size.Tag.ToString(), EncodeDecode.GetEncoded(Comb_Size.Text) });

                //Saving!!!
                string cpath = this.ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
                if (File.Exists(cpath))
                    File.Delete(cpath);
                DataSet ds = new DataSet();
                ds.Tables.Add(SumTbl);
                ds.WriteXml(cpath);
                La_LstSave.Text = DateTime.Now.ToString();              
            }
            catch {  }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        //Browse Image!
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {                
                OpenFileDialog of = new OpenFileDialog();
                of.Multiselect = false;
                of.Title = "Import Image For Current Project";                               // a  A
                of.Filter = "Image file|*.jpg;*.png;*.gif;*.bmp;*.jpeg|All files|*.*";       // b  B         b = aB/A
                if (of.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();
                    
                    //Save luon!                    
                    Image img = Image.FromFile(of.FileName);
                    if (PsPic.Image != null)
                        PsPic.Image.Dispose();
                    PsPic.Image = null;
                    //Save Image!
                    if (File.Exists(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE))
                        File.Delete(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE);
                    img.Save(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE);
                    LoadImage(of.FileName);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void button3_Click(object sender, EventArgs e)
        {          
            try
            {               
                if (File.Exists(ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE))
                    System.Diagnostics.Process.Start("rundll32.exe", "shimgvw.dll, ImageView_Fullscreen " + ProjectPath_ + Path.DirectorySeparatorChar + ProjectManager.PROJECT_IMG_FILE);
            }
            catch(Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}